/****************************************************************************
 * @file    : lin_config.c
 * @author  : Nonosns MCU Team
 * @version : V2.0
 * @Date    : 2023/4/7
 * @brief   : lin stack file
 * @note
 * Copyright (C) 2023 Novosense All rights reserved.
 ****************************************************************************/

/*******************************************************************************
 * Include files
 *******************************************************************************/
#include "lin_define.h"
#include "lin_config.h"

/*******************************************************************************
 * definitions
 *******************************************************************************/

/*******************************************************************************
 * external variables and constants
 *******************************************************************************/

const l_u8 g_serialNumber[4] = {
    L_SERIAL_NUMBER & 0xFF, // low byte first
    (L_SERIAL_NUMBER >> 8) & 0xFF,
    (L_SERIAL_NUMBER >> 16) & 0xFF,
    (L_SERIAL_NUMBER >> 24) & 0xFF};

const ProductIdentifier g_productIdentifier = {
    .supplierID = L_SUPPLIER_ID,
    .functionID = L_FUNCTION_ID,
    .variantID = L_VARIANT_ID};

#if !(defined L_LIN_2_2)
const l_u16 g_configMessageIDTable[L_ID_TABLE_SIZE - 2] = { // -2 because there is no message ID for ID_60 and ID_61
    0x0101,                                                 // supported message IDs
    0x0102,
    0x0103,
    0x0104,
    0x0105,
    0x0106};
#endif

// data buffer for the supported LIN frames
static l_u8 g_linFrameDataBufferOf_ID11[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}; // send 8 bytes to host
static l_u8 g_linFrameDataBufferOf_ID12[8];                                                    // receive 8 bytes from host
static l_u8 g_linFrameDataBufferOf_ID13[3];                                                    // receive 3 bytes from host
static l_u8 g_linFrameDataBufferOf_ID14[2] = {0xFF, 0xFF};                                     // send 2 bytes to host
static l_u8 g_linFrameDataBufferOf_ID17[8];                                                    // receive 4 byte from host (byte1：模式，byte2：命令，byte3-4：参数1，byte5-6：参数2，byte7：预留，byte8:crc。
static l_u8 g_linFrameDataBufferOf_ID18[8];                                                    // send 8 byte to host (故障、角度、当前位置adc高字节、当前位置adc低字节、最大位置adc高字节、最大位置adc低字节、0点位置adc高字节、0点位置adc低字节)
// add receive frames
// static l_u8 g_linFrameDataBufferOf_ID19[4];  // receive 4 byte from host (test data)
// add send frames
// static l_u8 g_linFrameDataBufferOf_ID20[8];  // send 8 byte to host (test data)

static l_u8 g_linFrameDataBufferOf_ID60[8]; // diagnosis request frame
static l_u8 g_linFrameDataBufferOf_ID61[8]; // diagnosis response frame

l_u8 g_nodeFramePIDTable[L_ID_TABLE_SIZE] = { // list of supported PIDs
    ID_DEFAULT,                               // configurable via LIN
    ID_DEFAULT,
    ID_13,
    ID_14,
    ID_17, // unconditional frame for Set Motor Pos
    ID_18, // unconditional frame for Get Motor Pos
           //    ID_19, // add receive frames ID for test
           //    ID_20, // add send frames ID for test
    ID_60, // ID_60 and ID_61 must be at the end of this table !!!
    ID_61};

const ConfigFrame g_nodeConfigFrameTable[L_ID_TABLE_SIZE] = {
    // attributes of the supported LIN frames
    {.direction = ID_DATASEND, .length = sizeof(g_linFrameDataBufferOf_ID11), .var = g_linFrameDataBufferOf_ID11},
    {.direction = ID_DATARECEIVE, .length = sizeof(g_linFrameDataBufferOf_ID12), .var = g_linFrameDataBufferOf_ID12},
    {.direction = ID_DATARECEIVE, .length = sizeof(g_linFrameDataBufferOf_ID13), .var = g_linFrameDataBufferOf_ID13},
    {.direction = ID_DATASEND, .length = sizeof(g_linFrameDataBufferOf_ID14), .var = g_linFrameDataBufferOf_ID14},
    {.direction = ID_DATARECEIVE, .length = sizeof(g_linFrameDataBufferOf_ID17), .var = g_linFrameDataBufferOf_ID17},
    {.direction = ID_DATASEND, .length = sizeof(g_linFrameDataBufferOf_ID18), .var = g_linFrameDataBufferOf_ID18},
    // ID_60 and ID_61 must be at the end of this table !!!
    {.direction = ID_DATARECEIVE, .length = sizeof(g_linFrameDataBufferOf_ID60), .var = g_linFrameDataBufferOf_ID60},
    {.direction = ID_DATASEND, .length = sizeof(g_linFrameDataBufferOf_ID61), .var = g_linFrameDataBufferOf_ID61},
};
